#!/usr/bin/env powershell

<#
.SYNOPSIS
    构建PyScreenBrain Docker镜像的脚本

.DESCRIPTION
    这个脚本用于构建PyScreenBrain项目的Docker镜像，包括检查Docker状态、构建镜像等功能

.EXAMPLE
    .\build_docker_image.ps1
    构建默认标签的镜像

    .\build_docker_image.ps1 -Tag "v1.0.0"
    构建指定标签的镜像

    .\build_docker_image.ps1 -BuildKit $false
    禁用BuildKit构建

.PARAMETER Tag
    镜像标签，默认为"latest"

.PARAMETER BuildKit
    是否使用BuildKit构建，默认为$true

.PARAMETER NoCache
    是否不使用缓存构建，默认为$false

.PARAMETER Pull
    是否总是拉取最新的基础镜像，默认为$false
#>

param(
    [string]$Tag = "latest",
    [bool]$BuildKit = $true,
    [bool]$NoCache = $false,
    [bool]$Pull = $false
)

# 设置错误处理
$ErrorActionPreference = "Stop"

# 函数：检查Docker是否安装
function Check-DockerInstalled {
    Write-Host "检查Docker是否安装..." -ForegroundColor Cyan
    try {
        docker --version | Out-Null
        Write-Host "✅ Docker已安装" -ForegroundColor Green
        return $true
    } catch {
        Write-Host "❌ Docker未安装，请先安装Docker Desktop" -ForegroundColor Red
        Write-Host "📥 下载地址：https://www.docker.com/products/docker-desktop" -ForegroundColor Yellow
        return $false
    }
}

# 函数：检查Docker是否正在运行
function Check-DockerRunning {
    Write-Host "检查Docker是否正在运行..." -ForegroundColor Cyan
    try {
        docker info | Out-Null
        Write-Host "✅ Docker正在运行" -ForegroundColor Green
        return $true
    } catch {
        Write-Host "❌ Docker未运行，请启动Docker Desktop" -ForegroundColor Red
        return $false
    }
}

# 函数：构建Docker镜像
function Build-DockerImage {
    param(
        [string]$ImageTag,
        [bool]$UseBuildKit,
        [bool]$NoCacheFlag,
        [bool]$PullFlag
    )

    Write-Host "开始构建Docker镜像..." -ForegroundColor Cyan
    
    # 设置构建参数
    $buildArgs = @()
    
    if ($UseBuildKit) {
        $env:DOCKER_BUILDKIT = "1"
        Write-Host "🔧 使用BuildKit构建" -ForegroundColor Yellow
    } else {
        $env:DOCKER_BUILDKIT = "0"
        Write-Host "🔧 不使用BuildKit构建" -ForegroundColor Yellow
    }
    
    if ($NoCacheFlag) {
        $buildArgs += "--no-cache"
        Write-Host "🔧 不使用缓存构建" -ForegroundColor Yellow
    }
    
    if ($PullFlag) {
        $buildArgs += "--pull"
        Write-Host "🔧 拉取最新基础镜像" -ForegroundColor Yellow
    }
    
    # 构建命令
    $buildCmd = @(
        "docker", "build",
        "-t", "pyscreenbrain:$ImageTag",
        "-f", "deploy/Dockerfile",
        $buildArgs,
        "."
    )
    
    Write-Host "📋 构建命令：" ($buildCmd -join " ") -ForegroundColor Gray
    
    try {
        & $buildCmd
        Write-Host "🎉 Docker镜像构建成功：pyscreenbrain:$ImageTag" -ForegroundColor Green
        return $true
    } catch {
        Write-Host "❌ Docker镜像构建失败：$_" -ForegroundColor Red
        return $false
    }
}

# 函数：显示构建结果
function Show-BuildResult {
    param(
        [bool]$Success,
        [string]$ImageTag
    )
    
    Write-Host "`n" -NoNewline
    Write-Host "=" * 60 -ForegroundColor Cyan
    if ($Success) {
        Write-Host "🎉 Docker镜像构建成功！" -ForegroundColor Green
        Write-Host "📦 镜像名称：pyscreenbrain:$ImageTag" -ForegroundColor Green
        Write-Host "`n💡 运行镜像命令：" -ForegroundColor Yellow
        Write-Host "   docker run -d -p 5000:5000 -p 5228:5228 -p 8931-8933:8931-8933 pyscreenbrain:$ImageTag" -ForegroundColor Gray
    } else {
        Write-Host "❌ Docker镜像构建失败！" -ForegroundColor Red
        Write-Host "📋 请检查以上错误信息并修复后重试" -ForegroundColor Yellow
    }
    Write-Host "=" * 60 -ForegroundColor Cyan
}

# 主函数
function Main {
    Write-Host "`n" -NoNewline
    Write-Host "🔥 PyScreenBrain Docker镜像构建脚本" -ForegroundColor Cyan
    Write-Host "=" * 60 -ForegroundColor Cyan
    
    # 检查Docker状态
    if (-not (Check-DockerInstalled)) {
        return
    }
    
    if (-not (Check-DockerRunning)) {
        return
    }
    
    # 构建镜像
    $buildSuccess = Build-DockerImage -ImageTag $Tag -UseBuildKit $BuildKit -NoCacheFlag $NoCache -PullFlag $Pull
    
    # 显示结果
    Show-BuildResult -Success $buildSuccess -ImageTag $Tag
}

# 执行主函数
Main